import React, {Component} from 'react'
import { Map, is } from 'immutable';
import PackageSetModal from '../../ListMdPackage/modal/PackageSetModal'
import {
    Button,
    Input,
    Icon,
    Select,
    Form,
    Row,
    Col,
    Radio,
    message,
    DatePicker,
} from 'antd';
import { SelectUrl } from '../../../components';
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
import moment from 'moment';
const dateFormat = 'YYYY-MM-DD HH:mm:ss'; //DatePicker格式
const formItemLayout = {
    labelCol: {
        span: 6
    },
    wrapperCol: {
        span: 14
    },
};
const buttonItemLayout = {
    wrapperCol: {
        span: 24,
        offset: 9,
    },
};

export default class ControlForm extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            isAdd: true, //办单默认是新增一条数据
            objectInfo: {}
        };
    }

    //提交事件
    saveHandle = (values) => {
        const options = {
            url: '/base/productcontrol/u',
            type: 'post',
            condition: values
        };
        fetchData(this, options, (resData) => {
            if (resData.code == "1") {
                message.success('保存成功!');
                this.resetHandle();//添加成功后重置表单
                this.props.refreshHandle();//添加成功后刷新list列表
            } else {
                message.error("请求数据错误：" + resData.message);
            }
        });
    }

    addHandle = (values) => {
        let searchCondition = this.props.searchCondition;//查询条件，创建新数据拼装上查询条件
        let addData = {...values, ...searchCondition}

        console.log("addData====", addData)

        const options = {
            url: '/base/productcontrol/c',
            type: 'post',
            condition: addData
        };
        fetchData(this, options, (resData) => {
            if (resData.code == "1") {
                message.success('新增成功!');
                this.resetHandle();//添加成功后重置表单
                this.props.refreshHandle();//添加成功后刷新list列表
            } else {
                message.error("请求数据错误：" + resData.message);
            }
        });
    }

    deleteHandle = () => {

        //TODO 执行删除事件

    }

    resetHandle = () => {
        this.props.form.resetFields();
    }
    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, fieldsValue) => {
            if (!err) {
                const values = {//有时间类型的需要再次处理
                    ...fieldsValue
                    //,
                    //'gmtCreateStr': fieldsValue['gmtCreateStr'].format('YYYY-MM-DD HH:mm:ss')
                }
                console.log('form表单值=====', values);
                if (this.state.isAdd) {//新增添加
                    this.addHandle(values);
                } else {//修改保存
                    this.saveHandle(values);
                }
            }
        });
    }

    componentWillReceiveProps(nextProps) {
        let blCompare = false;  //比较搜索条件是否变化，如果没有变化不执行查询
        const map1 = Map(this.props.rowObject);
        const map2 = Map(nextProps.rowObject);
        if (!is(map1, map2)) {
            blCompare = true;
        }
        if (blCompare) {
            this.props.form.resetFields();
            this.setState({
                isAdd: nextProps.isAdd,
                objectInfo: nextProps.rowObject
            });
        }
    }

    packageSearchClick = () => {
        this.refs.packageSetModal.showModal();
    }

    // 入库出库弹出模态框的统一回调函数
    setRuleFormData = (resultData) => {
        this.props.form.setFieldsValue(
            {
                packageCode: resultData
            }
        );
    }

    render() {
        const { getFieldDecorator, setFieldsValue } = this.props.form;
        let objectInfo = this.state.objectInfo;

        let actionButtons;
        if (this.state.isAdd) {//新增
            actionButtons = (

                <Row className="infoForm_row">
                    <FormItem {...buttonItemLayout}>
                        <Col span={2}>
                            <Button type='primary' htmlType="submit">新增</Button>
                        </Col>
                        <Col span={2}>
                            <Button type='default' onClick={this.resetHandle}>重置</Button>
                        </Col>
                    </FormItem>
                </Row>
            )
        } else {//修改、删除
            actionButtons = (
                <Row className="infoForm_row">
                    <FormItem {...buttonItemLayout}>
                        <Col span={2}>
                            <Button type='primary' htmlType="submit">保存</Button>
                        </Col>
                        <Col span={2}>
                            <Button type='danger' onClick={this.deleteHandle}>删除</Button>
                        </Col>
                        <Col span={2}>
                            <Button type='default' onClick={this.resetHandle}>重置</Button>
                        </Col>
                    </FormItem>
                </Row>
            )
        }
        return (
            <div className="infoForm">
                <Form onSubmit={this.handleSubmit}>
                    <FormItem style={{display: 'none'}}>
                        {
                            getFieldDecorator('productControlId', {
                                rules: [{required: false}],
                                initialValue: objectInfo.productControlId ? objectInfo.productControlId : ""
                            })(
                                <Input placeholder="请输入"/>
                            )}
                    </FormItem>

                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="产品代码">
                                {
                                    getFieldDecorator('productCode', {
                                        //rules: [{ required: true, message: '请输入产品代码', whitespace: true }],
                                        initialValue: this.props.searchCondition.productCode ? this.props.searchCondition.productCode : ""
                                    })(
                                        <Input disabled="true"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="仓库编码">
                                {
                                    getFieldDecorator('whCode', {
                                        //rules: [{ required: true, message: '请输入仓库编码', whitespace: true }],
                                        initialValue: objectInfo.whCode ? objectInfo.whCode : ""
                                    })(
                                        <Input disabled="true"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="客户编码">
                                {
                                    getFieldDecorator('custCode', {
                                        //rules: [{ required: true, message: '请输入客户编码', whitespace: true }],
                                        initialValue: objectInfo.custCode ? objectInfo.custCode : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="7">
                            <FormItem  {...formItemLayout} label="包装代码">
                                {
                                    getFieldDecorator('packageCode', {
                                        //rules: [{ required: true, message: '请输入包装代码', whitespace: true }],
                                        initialValue: objectInfo.packageCode ? objectInfo.packageCode : ""
                                    })(
                                        <Input placeholder="请输入" disabled="true" style={{width:'90%'}}/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="1">
                            <Icon type="search" style={{cursor:'pointer'}} onClick={this.packageSearchClick}/>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="产品仓储类型">
                                {
                                    getFieldDecorator('productTypeCode', {
                                        rules: [{ required: true, message: '请输入产品仓储类型', whitespace: true }],
                                        initialValue: objectInfo.productTypeCode ? objectInfo.productTypeCode : ""
                                    })(
                                        //<Input placeholder="请输入"/>
                                        <SelectUrl url='/base/dic/get/store_type' dicCode="dicCode" dicName="dicName" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="危险品标识">
                                {
                                    getFieldDecorator('dangerFlag', {
                                        //rules: [{ required: true, message: '请输入危险品标识', whitespace: true }],
                                        initialValue: objectInfo.dangerFlag ? objectInfo.dangerFlag : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="存储环境">
                                {
                                    getFieldDecorator('storageEnv', {
                                        //rules: [{ required: true, message: '请输入存储环境', whitespace: true }],
                                        initialValue: objectInfo.storageEnv ? objectInfo.storageEnv : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="收货方式">
                                {
                                    getFieldDecorator('takeGoodsWay', {
                                        //rules: [{ required: true, message: '请输入收货方式', whitespace: true }],
                                        initialValue: objectInfo.takeGoodsWay ? objectInfo.takeGoodsWay : ""
                                    })(
                                        //<Input placeholder="请输入"/>
                                        <SelectUrl url='/base/dic/get/delivery_type' dicCode="dicCode" dicName="dicName" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="批次属性">
                                {
                                    getFieldDecorator('batchCode', {
                                        //rules: [{ required: true, message: '请输入产品批次属性', whitespace: true }],
                                        initialValue: objectInfo.batchCode ? objectInfo.batchCode : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="库位指定规则">
                                {
                                    getFieldDecorator('locAssignWay', {
                                        //rules: [{ required: true, message: '请输入库位指定规则', whitespace: true }],
                                        initialValue: objectInfo.locAssignWay ? objectInfo.locAssignWay : ""
                                    })(
                                        //<Input placeholder="请输入"/>
                                        <SelectUrl url='/base/dic/get/asign_location_rule' dicCode="dicCode" dicName="dicName" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="上架规则">
                                {
                                    getFieldDecorator('shelfRule', {
                                        rules: [{required: true, message: '请输入上架规则', whitespace: true}],
                                        initialValue: objectInfo.shelfRule ? objectInfo.shelfRule : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="上架库位">
                                {
                                    getFieldDecorator('shelfLoc', {
                                        //rules: [{ required: true, message: '请输入产品上架库位', whitespace: true }],
                                        initialValue: objectInfo.shelfLoc ? objectInfo.shelfLoc : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="库存周转周期">
                                {
                                    getFieldDecorator('stockTurnDays', {
                                        rules: [{required: true, message: '请输入库存周转周期', whitespace: true}],
                                        initialValue: objectInfo.stockTurnDays ? objectInfo.stockTurnDays : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="分配规则">
                                {
                                    getFieldDecorator('distributeRule', {
                                        //rules: [{ required: true, message: '请输入分配规则', whitespace: true }],
                                        initialValue: objectInfo.distributeRule ? objectInfo.distributeRule : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="有效期控制">
                                {
                                    getFieldDecorator('validDateControl', {
                                        rules: [{required: true, message: '请输入有效期控制', whitespace: true}],
                                        initialValue: objectInfo.validDateControl ? objectInfo.validDateControl : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="有效期类型">
                                {
                                    getFieldDecorator('validDateType', {
                                        rules: [{required: true, message: '请输入有效期类型', whitespace: true}],
                                        initialValue: objectInfo.validDateType ? objectInfo.validDateType : ""
                                    })(
                                        //<Input placeholder="请输入"/>
                                        <SelectUrl url='/base/dic/get/valid_date_type' dicCode="dicCode" dicName="dicName" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="有效期天数">
                                {
                                    getFieldDecorator('validDate', {
                                        rules: [{required: true, message: '请输入有效期天数', whitespace: true}],
                                        initialValue: objectInfo.validDate ? objectInfo.validDate : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="失效期预警">
                                {
                                    getFieldDecorator('expiryDateWarn', {
                                        //rules: [{ required: true, message: '请输入失效期预警', whitespace: true }],
                                        initialValue: objectInfo.expiryDateWarn ? objectInfo.expiryDateWarn : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="入库效期">
                                {
                                    getFieldDecorator('instockPeriod', {
                                        //rules: [{ required: true, message: '请输入入库效期', whitespace: true }],
                                        initialValue: objectInfo.instockPeriod ? objectInfo.instockPeriod : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="出库效期">
                                {
                                    getFieldDecorator('outstockPeriod', {
                                        //rules: [{ required: true, message: '请输入出库效期', whitespace: true }],
                                        initialValue: objectInfo.outstockPeriod ? objectInfo.outstockPeriod : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="是否冻结">
                                {
                                    getFieldDecorator('freezeFlag', {
                                        //rules: [{ required: true, message: '请输入是否冻结', whitespace: true }],
                                        initialValue: objectInfo.freezeFlag ? objectInfo.freezeFlag : ""
                                    })(
                                        //<Input placeholder="请输入"/>
                                        <SelectUrl url='/base/dic/get/freeze_flag' dicCode="dicCode" dicName="dicName" />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="质检周期">
                                {
                                    getFieldDecorator('qcCycle', {
                                        //rules: [{ required: true, message: '请输入质检周期', whitespace: true }],
                                        initialValue: objectInfo.qcCycle ? objectInfo.qcCycle : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    {/*操作按钮*/}
                    {actionButtons}
                </Form>

                <PackageSetModal getTableData={this.setRuleFormData} ref='packageSetModal'/>
            </div>
        );
    }
}
